﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NewsType_edit : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            txtId.Attributes.Add("readonly", "readonly");
            bd();
        }
    }
    void bd()
    {
        string id = Request["id"];
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        ddlPid.DataSource = dal.NewsType.Where(x=>x.id!=id && string.IsNullOrEmpty(x.pid) );
        ddlPid.DataTextField = "name";
        ddlPid.DataValueField = "id";
        ddlPid.DataBind();
        ddlPid.Items.Insert(0, new ListItem("无", ""));
        CMSModel.NewsType type = dal.NewsType.First(x => x.id == id);
        txtId.Text = type.id;
        txtMemo.Text = type.memo;
        txtName.Text = type.name;
        ddlPid.SelectedValue = type.pid;
   
        if (type.enable == 1)
        {
            rbtnNormal.Checked = true;
        }
        else
        {
            rbtnForbidden.Checked = true;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        CMSModel.NewsType type = dal.NewsType.First(x => x.id == txtId.Text);
        type.ctime = DateTime.Now;
        type.enable = int.Parse(Request["type"]);
        type.id = txtId.Text;
        type.name = txtName.Text;
        type.memo = txtMemo.Text;
        type.pid = ddlPid.SelectedValue;
        if (string.IsNullOrEmpty(type.pid))
            type.pid = null;
        try
        {
            dal.SaveChanges();
    
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功'),document.location='Default.aspx'", true);
        }
        catch (Exception ex)
        {
            LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "addNewsType", LogYuMin.LogStates.ERROR, ex);
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加失败')", true);
        }
    }
}